interface ApiResponse<T = any> {
  success: boolean;
  data: T;
  message: string;
}

interface WeatherParams {
  city?: string;
  location?: string;
}

interface GeocodeParams {
  address?: string;
  location?: string;
  type?: 'geo' | 'regeo';
}

interface PoiParams {
  location: string;
  keywords?: string;
  radius?: number;
  types?: string;
}

interface RouteParams {
  origin: string;
  destination: string;
  strategy?: number;
  waypoints?: string;
}

export const useApi = () => {
  // 真实API调用方法
  const getWeather = async (params: WeatherParams): Promise<ApiResponse> => {
    const query = new URLSearchParams();
    if (params.city) query.append('city', params.city);
    if (params.location) query.append('location', params.location);
    
    return await $fetch(`/api/weather?${query.toString()}`);
  };

  const getGeocode = async (params: GeocodeParams): Promise<ApiResponse> => {
    const query = new URLSearchParams();
    if (params.address) query.append('address', params.address);
    if (params.location) query.append('location', params.location);
    if (params.type) query.append('type', params.type);
    
    return await $fetch(`/api/geocode?${query.toString()}`);
  };

  const getPoi = async (params: PoiParams): Promise<ApiResponse> => {
    const query = new URLSearchParams();
    query.append('location', params.location);
    if (params.keywords) query.append('keywords', params.keywords);
    if (params.radius) query.append('radius', params.radius.toString());
    if (params.types) query.append('types', params.types);
    
    return await $fetch(`/api/poi?${query.toString()}`);
  };

  const getRoute = async (params: RouteParams): Promise<ApiResponse> => {
    const query = new URLSearchParams();
    query.append('origin', params.origin);
    query.append('destination', params.destination);
    if (params.strategy) query.append('strategy', params.strategy.toString());
    if (params.waypoints) query.append('waypoints', params.waypoints);
    
    return await $fetch(`/api/route?${query.toString()}`);
  };

  // 模拟API调用方法
  const getWeatherMock = async (params: WeatherParams): Promise<ApiResponse> => {
    const query = new URLSearchParams();
    if (params.city) query.append('city', params.city);
    if (params.location) query.append('location', params.location);
    
    return await $fetch(`/api/weather-mock?${query.toString()}`);
  };

  const getGeocodeMock = async (params: GeocodeParams): Promise<ApiResponse> => {
    const query = new URLSearchParams();
    if (params.address) query.append('address', params.address);
    if (params.location) query.append('location', params.location);
    if (params.type) query.append('type', params.type);
    
    return await $fetch(`/api/geocode-mock?${query.toString()}`);
  };

  const getPoiMock = async (params: PoiParams): Promise<ApiResponse> => {
    const query = new URLSearchParams();
    query.append('location', params.location);
    if (params.keywords) query.append('keywords', params.keywords);
    if (params.radius) query.append('radius', params.radius.toString());
    if (params.types) query.append('types', params.types);
    
    return await $fetch(`/api/poi-mock?${query.toString()}`);
  };

  const getRouteMock = async (params: RouteParams): Promise<ApiResponse> => {
    const query = new URLSearchParams();
    query.append('origin', params.origin);
    query.append('destination', params.destination);
    if (params.strategy) query.append('strategy', params.strategy.toString());
    if (params.waypoints) query.append('waypoints', params.waypoints);
    
    return await $fetch(`/api/route-mock?${query.toString()}`);
  };

  // 通用HTTP方法
  const get = async <T = any>(url: string, params?: Record<string, any>): Promise<T> => {
    const query = params ? new URLSearchParams(params) : '';
    const fullUrl = query ? `${url}?${query.toString()}` : url;
    return await $fetch(fullUrl);
  };

  const post = async <T = any>(url: string, data?: any): Promise<T> => {
    return await $fetch(url, {
      method: 'POST',
      body: data
    });
  };

  const put = async <T = any>(url: string, data?: any): Promise<T> => {
    return await $fetch(url, {
      method: 'PUT',
      body: data
    });
  };

  const del = async <T = any>(url: string): Promise<T> => {
    return await $fetch(url, {
      method: 'DELETE'
    });
  };

  return {
    // 真实API方法
    getWeather,
    getGeocode,
    getPoi,
    getRoute,
    
    // 模拟API方法
    getWeatherMock,
    getGeocodeMock,
    getPoiMock,
    getRouteMock,
    
    // 通用HTTP方法
    get,
    post,
    put,
    del
  };
}; 